$(function () {

  $('input[name="identity"]').change(function (e) {
    var value = e.target.value
    var flag = value === 'personal'
    var $organizationName = $('input[name="organizationName"]')

    $('.organizationName').toggleClass('disabled')
    $organizationName.prop('disabled', flag)

    if (flag && $organizationName.val() !== '') {
      $organizationName.attr('data-value', $organizationName.val()).val('')
    }

    if (!flag && $organizationName.data('value') !== '') {
      $organizationName.val($organizationName.attr('data-value'))
    }
  })

  // 初始化所在地区
  initComplexArea('province', 'city', 'area', area_array, sub_array, '0', '0', '0');

  $('.submit-btn').click(function () {

    var formData = $('#form').serializeArray().reduce(function (prev, next) {
      prev[next.name] = next.value
      return prev
    }, {})

    if (formData.username === '') {
      wu.showMessage({
        title: "请输入姓名",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    if (formData.tell === '') {
      wu.showMessage({
        title: "请输入联系电话",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    // var tellReg = /^(?:(?:\+|00)86)?1\d{10}$/ // 宽松匹配正则
    // if (!tellReg.test(formData.tell)) {
    //   wu.showMessage({
    //     title: "请输入正确的联系电话",
    //     backgroundColor: '#ff4d4f',
    //     duration: 2500
    //   });
    //   return
    // }

    if (formData.email === '') {
      wu.showMessage({
        title: "请输入电子邮箱",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    var emailReg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if (!emailReg.test(formData.email)) {
      wu.showMessage({
        title: "请输入正确的电子邮箱",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    if (formData.identity === 'organization') {
      if (formData.organizationName === '') {
        wu.showMessage({
          title: "请输入机构名称",
          backgroundColor: '#ff4d4f',
          duration: 2500
        });
        return
      }
    }

    if (formData.province === '0') {
      wu.showMessage({
        title: "请选择省",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    if (formData.city === '0') {
      wu.showMessage({
        title: "请选择市",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    if (formData.area === '0') {
      wu.showMessage({
        title: "请选择区",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    if (formData.cooperationIntention === '') {
      wu.showMessage({
        title: "请输入合作意向",
        backgroundColor: '#ff4d4f',
        duration: 2500
      });
      return
    }

    var regionData = {
      provinceName: area_array[formData.province],
      provinceCode: formData.province,
      cityName: l_arr[formData.city],
      cityCode: formData.city,
      areaName: sub_arr[formData.city][formData.area],
      areaCode: formData.area
    }

    // 表单结果
    var resultObj = $.extend({}, formData, regionData)
    delete resultObj.province
    delete resultObj.city
    delete resultObj.area

    console.log(resultObj)

    window.location.href = 'submit-result.html'
  })

})